package com.forcedotcom.crypto
{
	import flash.utils.ByteArray;
	import com.hurlant.util.Hex;
	import com.hurlant.crypto.symmetric.PKCS5;
	import com.hurlant.crypto.symmetric.IPad;
	import com.hurlant.crypto.symmetric.ICipher;
	import com.hurlant.crypto.Crypto;
	
	import mx.rpc.events.HeaderEvent;

	public class ChatterEncryption
	{
		public function ChatterEncryption() {
		}
		
		private static var encryptionKey:String = "ThisIsTheWeakLink";
		
		public static function encrypt(stringToEncrypt:String):String {
			if (stringToEncrypt.length == 0) {
				return "";
			} else {
				var k:String = encryptionKey;
				var kdata:ByteArray = Hex.toArray(Hex.fromString(k));
				var txt:String = stringToEncrypt;
				var data:ByteArray = Hex.toArray(Hex.fromString(txt));;
				var name:String = "simple-aes-ecb";
				var pad:IPad = new PKCS5();
				var mode:ICipher = Crypto.getCipher(name, kdata, pad);
				pad.setBlockSize(mode.getBlockSize());
				mode.encrypt(data);
				return Hex.fromArray(data);
			}
		}
		
		public static function  decrypt(encryptedText:String):String {
			if (encryptedText.length == 0) {
				return "";
			} else {
				var k:String = encryptionKey;
				var kdata:ByteArray = Hex.toArray(Hex.fromString(k));
				var txt:String = encryptedText;
				var data:ByteArray = Hex.toArray(txt);
				var name:String = "simple-aes-ecb";
				var pad:IPad =new PKCS5();
				var mode:ICipher = Crypto.getCipher(name, kdata, pad);
				pad.setBlockSize(mode.getBlockSize());
				mode.decrypt(data);
				if (data==null) return null;
				var txtDecrypted:String = Hex.toString(Hex.fromArray(data));
				return txtDecrypted;
			}
		}
	}
}